import jsonschema

# 校验需求：
# -整个JSON数据是一个对象  "type":"object"
# -包含success、code、message、data字段，并且是必须存在的字段 required
# -success字段为布尔类型
# -code为整数
# -message为以"登录成功"结尾的字符串
# -data为对象，必须包含name,age字段
# -data中的name字段必须等于lily
# -data中的age必须是20
data = {
    "success": False,
    "code": 10000,
    "message": "xxx登录成功",
    "data": {
        "age": 20,
        "name": "lily"
    }
}

schema1 = {
    "type": "object",
    "properties": {
        "success": {"type": "boolean"},
        "code": {"type": "integer"},
        "message": {"pattern": "登录成功$"},
        "data": {
            "type": "object",
            "properties": {
                "age": {"const": 20},
                "name": {"const": "lily"}
            },
            "required": ["age", "name"]
        },
    },
    "required": ["success", "code", "message", "data"]
}
resp = jsonschema.validate(instance=data, schema=schema1)
print(resp)
